<?php

namespace AuctionBack\Controller;

use AuctionCore\Account\Back\Department;
use AuctionCore\Account\Back\Employee;
use AuctionCore\AuctionOrder\AuOrder;
use AuctionCore\CarResource\Car;
use AuctionCore\Cashier\Payment;
use AuctionCore\Common\AjaxPage;

/**
 * 付款单
 */
class PaymentController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
        $type_list = C("PAYMENT_TYPE");
        $ways = C("COLLECTION_WAY");
        $this->assign("type_list", $type_list);
        $this->assign("ways", $ways);
    }

    //首页
    public function index()
    {
        $branchId = $this->user_info['branch_id'];
        if($branchId == 0){
            $branchList = Department::getBranchCompanyList();
            $this->assign("branchList",$branchList);
        }else{
            $branchName = Department::getBranchCompanyByDepartId($branchId);
            $this->assign("branchName",$branchName);
        }
        $this->assign('branchId',$branchId);
        $this->display();
    }

    //收款列表
    public function getList()
    {
        $page = I("get.p/d", 1);
        $limit = 10;
        $t = I('get.t', 0);
        $car_no = I("get.car_no", '', 'trim');
        $pay_type = I('get.pay_type/d', 0);
        $payment_no = I('get.payment_no', '');
        $plate_number = I('get.plate_number', '');
        $branch_id = $this->user_info['branch_id'];
        if($branch_id == 0){
            $branch_id = I("get.branch_id/d", -1);
        }
        $params = [
            'status' => $t,
            'car_no' => $car_no,
            'branch_id' => $branch_id,
            'pay_type' => $pay_type,
            'payment_no' => $payment_no,
            'plate_number' => $plate_number,
        ];
        $result = Payment::getList($page, $limit, $params);
        $total = $result['total'] > 0 ? $result['total'] : 0;
        $list = empty($result['list']) ? array() : $result['list'];
        $Page = new AjaxPage($total, $limit, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $page_show = $Page->show();
        $this->assign('p', $page);
        $this->assign('t', $t);
        $this->assign('page', $page_show);
        $this->assign('params', $params);
        $this->assign('list', $list);
        $this->display('Payment/list');
    }

    //查看详情
    public function view()
    {
        $pid = I("get.pid/d", 0);
        if ($pid < 1) {
            $this->error("查找的信息不存在");
        }
        $p = new Payment($pid);
        $info = $p->getInfo();
        if ($info['status'] != 1) {
            $info['update_time'] = "--";
            $info['emp_name'] = "--";
        }
        if (in_array($info['pay_type'], [1, 2, 3])) {
            $purchaseOrderInfo = Payment::getPurchaseInfoById($info['id']);
            $info['modify_no'] = "--";
            $info['order_no'] = $purchaseOrderInfo['order_no'];
            $info['car_source_nu'] = $purchaseOrderInfo['car_source_nu'];
            $info['purchase_order_nu'] = $purchaseOrderInfo['purchase_order_nu'];
            $info['should_pay'] = $purchaseOrderInfo['should_pay'];
            $info['account_id'] = $purchaseOrderInfo['owner_id'];
            $this->assign('purchaseOrderInfo', $purchaseOrderInfo);
        } else {
            if ($info['refer'] > 0) {
                $table = $info['refer'] == 1 ? "breach_car" : "self_receive_car";
                $data = Payment::getCarBackInfoByPaymentId($info['id'], $table);
                $info['modify_no'] = "--";
            } else {
                $data = Payment::getChangeInfoById($info['id']);
                $info['modify_no'] = $data['modify_no'];
            }
            if ($info['pay_type'] == 7 || $info['pay_type'] == 4) {
                $order_fee = AuOrder::getOrderFee($data['order_id']);
                $this->assign("fee", $order_fee);
            }
            $info['order_no'] = $data['order_no'];
            $info['car_source_nu'] = $data['car_source_nu'];
            $info['purchase_order_nu'] = $data['purchase_order_nu'];
            $info['should_pay'] = $data['should_pay'];
            if ($info['pay_type'] == 6) {
                $info['account_id'] = $data['owner_id'];
            } else {
                $info['account_id'] = $data['success_dealer_id'];
            }
            $this->assign('changeInfo', $data);
        }
        $e = new Employee($this->user_info['emp_id']);
        $lockAuth = $e->check_auth("FuKuanDanSuoDing");
        $unlockAuth = $e->check_auth("FuKuanDanJieSuo");
        $this->assign("lockAuth", $lockAuth);
        $this->assign("unlockAuth", $unlockAuth);
        $this->assign("info", $info);
        $this->display();
    }

    //确认付款
    public function confirm()
    {
        $pid = I("post.id/d", 0);
        if ($pid < 1) {
            $this->helpJsonResult(400, '参数错误');
        }
        $pay = new Payment($pid);
        $paymentInfo = $pay->getInfo();
        if (empty($paymentInfo)) {
            $this->helpJsonResult(400, "付款信息不存在");
        }
        if ($paymentInfo['status'] != 0) {
            $this->helpJsonResult(400, "状态不正确");
        }
        $data = [];
        $data['status'] = 1;
        $data['emp_id'] = $this->user_info['emp_id'];
        $data['emp_name'] = $this->user_info['real_name'];
        $data['update_time'] = date('Y-m-d H:i:s');
        $data['pay_type'] = $paymentInfo['pay_type'];
        $data['refer'] = $paymentInfo['refer'];

        $res = $pay->confirmPayment($data);
        if (!$res) {
            $this->helpJsonResult(500, "确认失败");
        }
        $this->helpJsonResult(200, "确认成功");
    }

    public function lock()
    {
        $pid = I("post.id/d", 0);
        if ($pid < 1) {
            $this->helpJsonResult(400, '参数错误');
        }
        $pay = new Payment($pid);
        $paymentInfo = $pay->getInfo();
        if (empty($paymentInfo)) {
            $this->helpJsonResult(400, "付款信息不存在");
        }
        if ($paymentInfo['status'] != 0) {
            $this->helpJsonResult(400, "状态不正确");
        }
        $emp_id = $this->user_info['emp_id'];
        $emp_name = $this->user_info['real_name'];
        $res = Payment::lock($pid, $emp_id, $emp_name);
        if (!$res) {
            $this->helpJsonResult(500, "锁定失败");
        }
        $this->helpJsonResult(200, "锁定成功");
    }

    public function unlock()
    {
        $pid = I("post.id/d", 0);
        if ($pid < 1) {
            $this->helpJsonResult(400, '参数错误');
        }
        $pay = new Payment($pid);
        $paymentInfo = $pay->getInfo();
        if (empty($paymentInfo)) {
            $this->helpJsonResult(400, "付款信息不存在");
        }
        if ($paymentInfo['status'] != 2) {
            $this->helpJsonResult(400, "状态不正确");
        }
        $emp_id = $this->user_info['emp_id'];
        $emp_name = $this->user_info['real_name'];
        $res = Payment::unlock($pid, $emp_id, $emp_name);
        if (!$res) {
            $this->helpJsonResult(500, "解锁失败");
        }
        $this->helpJsonResult(200, "解锁成功");
    }
}
